﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using System.Security.Cryptography;
using Microsoft.Win32;
using System.Text.RegularExpressions;
using System.Net;
using Excel = Microsoft.Office.Interop.Excel;

using System.Data.SQLite;

namespace LuZhouLaoJiao
{
    class Global
    {
        //static public string dbUser = "root";
        //static public string dbPwd
        //static public string database = "lzljtest";
        //public static string dbUser = getRegeditValue("localDBUser");
        //public static string dbPwd = getRegeditValue("localDBPwd");
        //public static string database = getRegeditValue("localDatabase");
        static public string dbUser = null;
        static public string dbPwd = null;
        static public string database = null;
        static public string localDBIP = null;
        static public string jiaochihao = null;
        //static public string tempFilePath = "C:/temp";  //与控制程序交互数据的文件
        static public string dataExchange = "./数据交换.txt";
        static public int pageSize = 15;    //每页显示的数据条数
        static public int sleepTime = 5000; //界面程序读取数据间隔的时间
        static public bool inquiried = false;   //判断从


        public static string userName
        {
            get;
            set;
        }

        public static string MD5(string str)
        {
            MD5 m = new MD5CryptoServiceProvider();
            byte[] s = m.ComputeHash(UnicodeEncoding.UTF8.GetBytes(str));
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < s.Length; i++)
            {
                sb.Append(s[i].ToString("x2"));
            }
            return sb.ToString();

        }

        //判断用户是否存在
        public static bool isExist(string strUserName, string strPassWord)
        {
            //dbUser = getRegeditValue("localDBUser");
            //dbPwd = getRegeditValue("localDBPwd");
            //database = getRegeditValue("localDatabase");
            //localDBIP = getRegeditValue("localIP");
            //bool Result = false;
            //string strCon = "server = "+ localDBIP +"; user id = "+ dbUser + "; password = "+ dbPwd +"; database = " + database +"; persist security info = false";
            //MySqlConnection conn = new MySqlConnection(strCon);
            //MySqlCommand comm = new MySqlCommand();
            //comm.Connection = conn;
            //comm.CommandText = "select count(*) from user where (name='"+strUserName+"') and (pwd='"+strPassWord+"');";
            //try
            //{
            //    conn.Open();
            //    int i = Convert.ToInt32(comm.ExecuteScalar());
            //    if (i > 0)
            //    {
            //        Result = true;
            //    }
            //    else
            //    {
            //        Result = false;
            //    }
            //    return Result;
            //}
            //catch (System.Exception ex)
            //{
            //    MessageBox.Show("数据库读取错误(Gl)：" + ex.Message);
            //    return false;
            //}
            //finally
            //{
            //    conn.Close();
            //}

            SQLiteConnection conn = new SQLiteConnection("Data Source=./data.db");
            SQLiteCommand comm = new SQLiteCommand();
            //MessageBox.Show("sqlite");
            conn.Open();
            comm = conn.CreateCommand();
            comm.CommandText = "select count(*) from user where (name='" + strUserName + "') and (pwd='" + strPassWord + "');";
            int i = Convert.ToInt32(comm.ExecuteScalar());
            conn.Close();
            if (i > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
            //comm.ExecuteNonQuery();
            
        }
        
        //判断用户是否存在
        public static bool isUserExist(string strUserName)
        {
            //dbUser = getRegeditValue("localDBUser");
            //dbPwd = getRegeditValue("localDBPwd");
            //database = getRegeditValue("localDatabase");
            //localDBIP = getRegeditValue("localIP");
            //bool Result = false;
            //string strCon = "server = " + localDBIP + "; user id = " + dbUser + "; password = " + dbPwd + "; database = " + database + "; persist security info = false";
            //MySqlConnection conn = new MySqlConnection(strCon);
            //MySqlCommand comm = new MySqlCommand();
            //comm.Connection = conn;
            //comm.CommandText = "select count(*) from user where (name='" + strUserName + "');";
            //try
            //{
            //    conn.Open();
            //    int i = Convert.ToInt32(comm.ExecuteScalar());
            //    if (i > 0)
            //    {
            //        Result = true;
            //    }
            //    else
            //    {
            //        Result = false;
            //    }
            //    return Result;
            //}
            //catch (System.Exception ex)
            //{
            //    MessageBox.Show("数据库读取错误(Gl)：" + ex.Message + dbUser);
            //    return false;
            //}
            //finally
            //{
            //    conn.Close();
            //}

            SQLiteConnection conn = new SQLiteConnection("Data Source=./data.db");
            SQLiteCommand comm = new SQLiteCommand();
            conn.Open();
            comm = conn.CreateCommand();
            comm.CommandText = "select count(*) from user where (name='" + strUserName + "');";
            int i = Convert.ToInt32(comm.ExecuteScalar());
            conn.Close();
            if (i > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        //向注册表中添加项
        public static void setRegeditValue(string name, string value)
        {
            RegistryKey hkey = Registry.CurrentUser;
            RegistryKey software = hkey.OpenSubKey("SOFTWARE", true);
            RegistryKey lzlj = software.CreateSubKey("LZLJ");
            lzlj.SetValue(name, value);
            lzlj.Flush();
            hkey.Close();
        }

        //获取注册表中项的值
        public static string getRegeditValue(string name)
        {
            RegistryKey hkey = Registry.CurrentUser;
            RegistryKey lzlj = hkey.OpenSubKey("Software\\LZLJ");
            string str = lzlj.GetValue(name).ToString();
            
            hkey.Close();
            return str;
        }

        //判断注册表中指定的项是否存在
        public static bool isKeyExist(string name)
        {
            RegistryKey hkey = Registry.CurrentUser;
            RegistryKey lzlj = hkey.OpenSubKey("Software\\LZLJ");
            try
            {
                string value = lzlj.GetValue(name).ToString();
                if (value != null)
                {
                    return true;
                }
            }
            catch (System.NullReferenceException ex)
            {
                return false;
            }
            finally
            {
                hkey.Close();
            }
            return false;
        }

        //删除注册表中指定的项
        public static void deleteKeyValue(string name)
        {
            RegistryKey hkey = Registry.CurrentUser;
            RegistryKey software = hkey.OpenSubKey("SOFTWARE", true);
            RegistryKey lzlj = software.OpenSubKey("LZLJ", true);
            
            try
            {
                lzlj.DeleteValue(name, false);
                lzlj.Flush();
            }
            catch (System.ArgumentException ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                hkey.Close();
            }
        }

        public static bool IsIP(string ip)
        {
            //判断是否为IP
            return Regex.IsMatch(ip, @"^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$");
        }

        public static string getLocalIP() 
        {
            string localIP = null;
            IPAddress[] ipList = Dns.GetHostEntry(Dns.GetHostName()).AddressList;
            foreach (IPAddress ip in ipList)
            {
                if (IsIP(ip.ToString()))
                    localIP = ip.ToString();
            }
            return localIP;
        }
        public static string headerMatch(string header)
        {
            Dictionary<string, string> dic = new Dictionary<string, string>()
            {
                {"批次号", "pici_id"},
                {"平均酸度", "suandu"},
                {"检测人", "jianceren"},
                {"样本来源", "laiyuan"},
                {"检测日期", "riqi"},
                {"检测时间", "shijian"},
                {"备注", "beizhu"},
                {"样本号", "yangben_id"},
                {"窖池号", "jiaochi"},
                {"酸度", "suandu"},
                {"机器号", "jiqi"},
                {"已提交", "tijiao"},
            };
            return dic[header];
        }



        public static bool DataGridviewShowToExcel(DataGridView dgv, bool isShowExcle)
        {
            if (dgv.Rows.Count == 0)
                return false;
            //建立Excel对象   
            Excel.Application excel = new Excel.Application();
            Excel.Workbook wb = excel.Application.Workbooks.Add(true);
            excel.Visible = isShowExcle;
            //生成字段名称
            for (int i = 0; i < dgv.ColumnCount; i++)
            {
                excel.Cells[1, i + 1] = dgv.Columns[i].HeaderText;
            }
            //填充数据.
            for (int i = 0; i < dgv.RowCount - 1; i++)
            {
                for (int j = 0; j < dgv.ColumnCount; j++)
                {
                    if (dgv[j, i].ValueType == typeof(string))
                    {
                        excel.Cells[i + 2, j + 1] = "'" + dgv[j, i].Value.ToString();
                    }
                    else
                    {
                        excel.Cells[i + 2, j + 1] = dgv[j, i].Value.ToString();
                    }
                }
            }
            return true;
        }

        public static bool isNumber(string num)
        {
            try
            {
                Decimal number = Convert.ToDecimal(num);
                return true;
            }
            catch (System.Exception ex)
            {
                return false;
            }
        }
    }

}


